package com.leo.controller;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
public class MessageController {

    /**
     * AmqpTemplate 是 RabbitTemplate 的父类接口
     * 方式1
     */
   /* @Autowired
    private AmqpTemplate rabbitTemplate;*/


    /**
     * 方式2
     */
    private RabbitTemplate rabbitTemplate;
    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        /**发送端确认ack，参数和原生的有些不一样,但前两个是一样的*/
        this.rabbitTemplate.setConfirmCallback((correlationData, flag, cause) -> {
            if (flag) {
                try {
                    System.out.println("消息确认发送成功:" +
                            correlationData.getId() + "  "
                            + new
                            String(correlationData.getReturnedMessage().getBody(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println(cause);
            } });
    }


    @RequestMapping("/biz")
    public String doBiz() throws UnsupportedEncodingException {
        MessageProperties props = new MessageProperties();
        props.setCorrelationId("1234");
        props.setConsumerTag("msg1");
        props.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        props.setContentEncoding("utf-8");

        /**可设置消息是否持久化*/
        //props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        //props.setDeliveryMode(MessageDeliveryMode.PERSISTENT);

        /**CorrelationData是ack确认时，需要返回的信息*/
        CorrelationData cd = new CorrelationData();
        cd.setId("msg1");
        cd.setReturnedMessage(new Message("这是msg1的响应".getBytes("utf-8"), null));
        Message message = new Message("这是等待确认的消息".getBytes("utf-8"), props);
        rabbitTemplate.convertAndSend("ex.biz", "biz", message, cd);
        return "ok";
    }


    @RequestMapping("/rabbit/{message}")
    public String receive(@PathVariable String message) throws UnsupportedEncodingException {

        final MessageProperties messageProperties = MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("utf-8")
                .setHeader("hello", "world")
                .build();

        final Message msg = MessageBuilder
                .withBody(message.getBytes("utf-8"))
                .andProperties(messageProperties)
                .build();

        rabbitTemplate.send("ex.boot", "key.boot", msg);

        return "ok";
    }

}
